package com.ymttest.business.data;

import java.io.IOException;
import java.security.PrivateKey;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.ymt.base.BaseBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.core.tool.RSAUtils;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

public class Sign {
	/**
	 * appid tradeService专用掩码
	 */
	public static final String autoTestKey = "autotestsalt";

	public static final String weixinAppKey = "c5781df6b8f149adca6094cdac4ac684";
	public static final String weixinJSAPIKey = "es839gnc8451lp0s943n568xzskjgdbv";

	public static final String newweixinAppKey = "autotestsalt";
	public static final String newweixinJSAPIKey = "autotestsalt";
	public static final String sftTestKey = "autotestsalt";

	/**
	 * 格式化BaseBean为HttpString key=value& 格式 使用treemap排序
	 * 
	 * @param bb
	 *            BaseBean
	 * @return
	 */

	public static String formatByBaseBean(BaseBean bb) {
		String p = "";
		TreeMap tm = (TreeMap) FormatUtil.GSONToObject(FormatUtil.beanToGSONString(bb), TreeMap.class);
		Iterator iterator = tm.keySet().iterator();
		while (iterator.hasNext()) {
			String name = iterator.next().toString();
			String value = tm.get(name).toString();
			if (null != value) {
				p += name + "=" + value.toString() + "&";
			}
		}
		if (p.length() > 1) {
			p = p.substring(0, p.length() - 1);
		}
		Logger.debug("formatByBaseBean:" + p);
		return p;
	}

	/**
	 * Map转换成key=value
	 * 
	 * @param m
	 * @return
	 */
	public static String formatByMap(Map m) {
		String p = "";
		Iterator iterator = m.keySet().iterator();
		while (iterator.hasNext()) {

			String name = iterator.next().toString();
			String value = m.get(name).toString();
			if (null != value) {
				p += name + "=" + value.toString() + "&";
			}
		}
		if (p.length() > 1) {
			p = p.substring(0, p.length() - 1);
		}
		Logger.debug("formatByMap:" + p);
		return p;
	}

	/**
	 * md5加签，格式化BaseBean为HttpString key=value& 格式 使用treemap排序 最后加key后md5
	 * 
	 * @param bb
	 *            要发送的参数bean
	 * @param key
	 *            掩码
	 * @return
	 */
	public String signMd5(BaseBean bb, String key) {
		String md5 = null;
		String formatString = formatByBaseBean(bb);
		formatString += key;
		Logger.debug("formatString:"+formatString);
		md5 = TestDataManager.getMD5(formatString.getBytes()).replace("-", "");
		return md5;
	}

	/**
	 * md5加签 格式化Map为HttpString key=value& 格式 使用treemap排序 然后转换为
	 * LinkedHashMap末尾put key 最后md5
	 * 
	 * @param m
	 *            要发送的Map
	 * @param key
	 *            掩码
	 * @return
	 */
	public String signMd5(Map m, String key) {
		LinkedHashMap tm = new LinkedHashMap();
		tm.putAll(m);
		tm.put("key", key);
		String md5 = null;
		String formatString = formatByMap(tm);
		md5 = TestDataManager.getMD5(formatString.getBytes()).replace("-", "");
		return md5;
	}

	/**
	 * md5加签 格式化Map为HttpString key=value& 格式 使用treemap排序 然后转换为
	 * LinkedHashMap末尾put key最后md5(不会添加&key=)
	 * 
	 * @param m
	 *            要发送的Map
	 * @param key
	 *            掩码
	 * @return
	 */
	public String signMd5NoAndKey(Map m, String key) {
		LinkedHashMap tm = new LinkedHashMap();
		tm.putAll(m);
		// tm.put("key", key);
		String md5 = null;
		String formatString = formatByMap(tm);
		formatString += key;
		md5 = TestDataManager.getMD5(formatString.getBytes()).replace("-", "");
		return md5;
	}

	/**
	 * cmb用 格式化Map为HttpString key=value& 格式 使用treemap排序 然后转换为 LinkedHashMap末尾put
	 * key 最后SignSHA256
	 * 
	 * @param m
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public String signSHA256(Map m, String key) throws Exception {
		LinkedHashMap tm = new LinkedHashMap();
		tm.putAll(m);
		String s256 = null;
		String formatString = formatByMap(tm);
		formatString = formatString + "&" + key;
		s256 = RSAUtils.SignSHA256(formatString);
		return s256;
	}
	/**
	 * 兼容旧模式
	 * @param src
	 * @return
	 * @throws Exception
	 */
	public static String SignSHA1WithRSA(String src) throws Exception {
		return SignSHA1WithRSA(src, true);
	}
	/**
	 * 对源数据SignSHA1WithRSA加签
	 * 
	 * @param src
	 *            源数据
	 * @return
	 * @throws Exception
	 */
	public static String SignSHA1WithRSA(String src, boolean sort) throws Exception {
		PrivateKey privateKey = RSAUtils.loadPrivateKey(EnvSetup.getData("privateK"));
		if (sort) {
			TreeMap tm = new TreeMap();
			tm.putAll((FormatUtil.httpStringToMap(src)));
			String fromatS = FormatUtil.mapToHttpString(tm, false);
			String signSWR = RSAUtils.SignSHA1WithRSA(fromatS, privateKey);
			return signSWR;
		} else {
			String signSWR = RSAUtils.SignSHA1WithRSA(src, privateKey);
			return signSWR;
		}

	}

	public static void main(String args[]) throws IOException {
		List<String> s = TestDataManager.getLines("seller.dat");
		for (String string : s) {
			Logger.debug(string);
			TestDataManager.appendFileToLine("seller1.dat", FormatUtil.urlEncode(string.trim()));
		}
		// Logger.debug(FormatUtil.urlEncode("11cfce10-8723-4fa2-9715-c98d8dcf92c4"));
		// AcquireOrderBean bb = new AcquireOrderBean();
		// bb.setAppId("tradeService");
		// bb.setBankId("CMB");
		// bb.setBizCode("3");
		// bb.setCallbackUrl("http://www.ymatou.com/pay/result");
		// bb.setCurrency("CNY");
		// bb.setEncoding("65001");
		// bb.setExt("{\"SHOWMODE\":\"2\",\"PAYMETHOD\":\"2\",
		// \"IsHangZhou\":1}");
		// bb.setNotifyUrl("http://api.trading.operate.ymatou.com/api/Trading/TradingCompletedNotify");
		// bb.setOrderId("20160411193515578");
		// bb.setOrderTime("20160411193515");
		// bb.setOriginAppId("originAppId");
		// bb.setPayPrice("10");
		// bb.setPayType("13");
		// bb.setProductName("你好");
		// bb.setSignMethod("MD5");
		// bb.setTraceId("2a0baebb-4944-45d2-9c66-a03f33b38a7c");
		// bb.setUserId("20124373");
		// bb.setUserIp("127.0.0.1");
		// bb.setVersion("1");
		// Logger.debug(new Sign().signMd5(bb, Sign.autoTestKey));
	}

}
